package cc.yugu.yhdw.webserver.web.controller.security.hdw;

import cc.yugu.yhdw.common.annotation.SecurityControl;
import cc.yugu.yhdw.common.annotation.ViewModel;
import cc.yugu.yhdw.common.domain.basic.User;
import cc.yugu.yhdw.common.domain.hdw.ExchangePrice;
import cc.yugu.yhdw.common.entity.json.ExtResult;
import cc.yugu.yhdw.common.entity.json.PageResult;
import cc.yugu.yhdw.webserver.constant.AppConstEnum;
import cc.yugu.yhdw.webserver.service.basic.AgentService;
import cc.yugu.yhdw.webserver.service.hdw.AgencyService;
import cc.yugu.yhdw.webserver.service.hdw.ExchangePriceService;
import cc.yugu.yhdw.webserver.web.controller.security.SecurityController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * 换电套餐控制类
 * 
 * @Date:Created in 2018/7/31 11:03
 * @Author:
 * @Modified By: zjn 2018/7/31 11:03
 */ 
@Controller
@RequestMapping(value = "/security/hdw/exchange_price")
public class ExchangePriceController extends SecurityController {

    @Autowired
    ExchangePriceService exchangePriceService;
    @Autowired
    AgentService agentService;
    // 新增注入
    @Autowired
    AgencyService agencyService;

    @RequestMapping(value = "index.htm")
    public void index(Model model) {
        // 换电类型
        model.addAttribute("ActiveTypeEnum", ExchangePrice.ActiveType.values());
        // 套餐类型
        model.addAttribute("TypeEnum", ExchangePrice.Type.values());
        model.addAttribute(MENU_CODE_NAME, AppConstEnum.Menu.MENU_02_04.getValue());
    }
    /**
     * 分页查询（新增：权限不同，查询不同）
     * 
     * @Date:Created in 2018/7/31 16:34
     * @Author:
     * @Modified By: zjn 2018/7/31 16:34
     */ 
    @RequestMapping("page.htm")
    @ViewModel(ViewModel.JSON)
    @ResponseBody
    public PageResult page(ExchangePrice search, HttpServletRequest request){
        /*
          页面点击 '所有' 搜索时，其实传过来的值为 0 ， 当点击搜索时，search里面存的有运营商或代理商的搜索值
         */
//        if (search.getAgencyId() == null ) {
//            search.setAgencyId(null);
//            return PageResult.successResult(exchangePriceService.findPage(search));
//        }


        if (search.getAgencyId() != null && search.getAgencyId() == 0) {
            search.setAgencyId(null);
        }
        if (search.getAgentId() != null && search.getAgentId() == 0) {
            search.setAgentId(null);
        }
        // 如果是运营商登录
        if(getSessionUser(request).getType() == User.Type.AGENT.getValue()){
            search.setAgentId(getSessionUser(request).getAgentId());
        }else if(getSessionUser(request).getType() == User.Type.AGENCY.getValue()){
            search.setAgencyId(getSessionUser(request).getAgencyId());
        }
//        if(getSessionUser(request).getAgentId() !=0 &&getSessionUser(request).getAgencyId() == 0) {
//            search.setAgentId(getSessionUser(request).getAgentId());
//            search.setAgencyId(null);
//            return PageResult.successResult(exchangePriceService.findPage(search));
//        }
//        if(getSessionUser(request).getAgentId() !=0 &&getSessionUser(request).getAgencyId() != 0) {
//            search.setAgentId(getSessionUser(request).getAgentId());
//            search.setAgencyId(getSessionUser(request).getAgencyId());
//            return PageResult.successResult(exchangePriceService.findPage(search));
//        }
//        if(getSessionUser(request).getAgentId() == 0&&getSessionUser(request).getAgencyId() == 0) {
//            search.setAgentId(null);
//            search.setAgencyId(null);
//            return PageResult.successResult(exchangePriceService.findPage(search));
//        }
        return PageResult.successResult(exchangePriceService.findPage(search));
    }
    /**
    * 页面 查看
    * 
    * @Param: [model, id]
    * @Return: java.lang.String
    * @Author By: zjn 2018/7/31  14:51
    */ 
    @RequestMapping(value = "view.htm", method = RequestMethod.GET)
    public String view(Model model, Long id) {
        ExchangePrice entity = exchangePriceService.find(id);
        if(entity == null) {
            return SEGMENT_RECORD_NOT_FOUND;
        }
        model.addAttribute("entity", entity);
        return "/security/hdw/exchange_price/view";
    }
    /**
    * 查看->基本信息查看
    * 
    * @Param: [model, id]
    * @Return: java.lang.String
    * @Author By: zjn 2018/7/31  14:53
    */ 
    @RequestMapping(value = "view_basic.htm")
    public String viewBasic(Model model, Long id) {
        ExchangePrice entity = exchangePriceService.find(id);
        if(entity == null) {
            return SEGMENT_RECORD_NOT_FOUND;
        } else {
            Integer agentId = entity.getAgentId();
            Integer agencyId = entity.getAgencyId();
            if (agentId != null) {
                entity.setAgentName(agentService.find(agentId).getAgentName());
            }
            // 设置代理商名称
            if (agencyId != null){
                entity.setAgencyName(agencyService.find(agencyId).getAgencyName());
            }
            model.addAttribute("entity", entity);
            model.addAttribute("ActiveTypeEnum", ExchangePrice.ActiveType.values());
            model.addAttribute("TypeEnum", ExchangePrice.Type.values());
        }
        return "/security/hdw/exchange_price/view_basic";
    }

    @ViewModel(ViewModel.INNER_PAGE)
    @RequestMapping(value = "add.htm", method = RequestMethod.GET)
    public void add(Model model) {
        model.addAttribute("ActiveTypeEnum", ExchangePrice.ActiveType.values());
        model.addAttribute("TypeEnum", ExchangePrice.Type.values());
    }

    @ViewModel(ViewModel.JSON)
    @RequestMapping(value = "create.htm",method = RequestMethod.POST)
    @ResponseBody
    public ExtResult insert(ExchangePrice entity){
        // 新建套餐信息插入（含代理商）
        return exchangePriceService.createAgencyId(entity);
    }

    /**
    * 修改
    * 
    * @Param: [model, id]
    * @Return: java.lang.String
    * @Author By: zjn 2018/7/31  13:37
    */ 
    @RequestMapping(value = "edit.htm", method = RequestMethod.GET)
    public String edit(Model model, Long id) {
        ExchangePrice entity = exchangePriceService.find(id);
        if(entity == null) {
            return SEGMENT_RECORD_NOT_FOUND;
        }
        model.addAttribute("entity", entity);
        return "/security/hdw/exchange_price/edit";
    }
    /**
    * 修改->基本信息修改
    * 
    * @Param: [model, id]
    * @Return: java.lang.String
    * @Author By: zjn 2018/7/31  13:49
    */ 
    @RequestMapping(value = "edit_basic.htm")
    public String editBasic(Model model, Long id) {
        ExchangePrice entity = exchangePriceService.find(id);
        if(entity == null) {
            return SEGMENT_RECORD_NOT_FOUND;
        } else {
            model.addAttribute("entity", entity);
            model.addAttribute("ActiveTypeEnum", ExchangePrice.ActiveType.values());
            model.addAttribute("TypeEnum", ExchangePrice.Type.values());
        }
        return "/security/hdw/exchange_price/edit_basic";
    }

    @RequestMapping("update_basic.htm")
    @ViewModel(ViewModel.JSON)
    @ResponseBody
    public ExtResult updateBasic(ExchangePrice entity) {
        return exchangePriceService.updateAgencyId(entity);
    }

    @ResponseBody
    @ViewModel(ViewModel.JSON)
    @RequestMapping(value = "delete.htm")
    public ExtResult delete(Long id) {
        return exchangePriceService.delete(id);
    }
    
    /**
    * 查询基础套餐信息
    * 
    * @Param: [model, agentId, agencyId]
    * @Return: void
    * @Author By: zjn 2018/8/1  15:21
    */ 
    @ViewModel(ViewModel.INNER_PAGE)
    @RequestMapping(value = "select_base_price.htm")
    public void selectBasePrice(Model model, Integer agentId,Integer agencyId) {
        model.addAttribute("ActiveTypeEnum", ExchangePrice.ActiveType.values());
        model.addAttribute("TypeEnum", ExchangePrice.Type.values());
        model.addAttribute("agentId", agentId);
        model.addAttribute("agencyId",agencyId);
    }

    @ViewModel(ViewModel.INNER_PAGE)
    @RequestMapping(value = "select_activity_price.htm")
    public void selectActivityPrice(Model model, int agentId,Integer agencyId) {
        model.addAttribute("ActiveTypeEnum", ExchangePrice.ActiveType.values());
        model.addAttribute("TypeEnum", ExchangePrice.Type.values());
        model.addAttribute("agentId", agentId);
        model.addAttribute("agencyId",agencyId);
    }

    @ViewModel(ViewModel.INNER_PAGE)
    @RequestMapping(value = "select_price.htm")
    public void selectPrice(Model model, Long agentId) {
        model.addAttribute("agentId", agentId);
    }

}
